package com.authine.cloudpivot.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Set;

public class StringUtil {

    /**
     * 将集合转化为 '1','2' 这种字符串格式
     * */
    public static String _StringTa(String [] str){
        StringBuffer sub = new StringBuffer();
        if (null !=str && str.length >0){
            for (String key : str) {
                sub.append("'"+key+"',");
            }
            String rc = sub.substring(0,sub.length()-1);
            return rc;
        }
        return null;
    }

    /**
     * 将集合转化为 '1','2' 这种字符串格式
     * */
    public static String _StringTa(Set<String> str){
        StringBuffer sub = new StringBuffer();
        if (null !=str && str.size() >0){
            for (String key : str) {
                sub.append("'"+key+"',");
            }
            String rc = sub.substring(0,sub.length()-1);
            return rc;
        }
        return null;
    }


    /**
     * 将字符串的首字母转大写
     * @param str 需要转换的字符串
     * @return
     */
    public static String captureName(String str) {
        // 进行字母的ascii编码前移，效率要高于截取字符串进行转换的操作
        char[] cs=str.toCharArray();
        cs[0]-=32;
        return String.valueOf(cs);
    }

    public static String lowerFirst(String str) {
        char[] cs=str.toCharArray();
        cs[0]+=32;
        return String.valueOf(cs);
    }

    /**
     * 转换部门和选人控件
     * 保留格式 ["id":"","type":type]
     * */
    public static String retainForm(String info){

        if (StringUtils.isEmpty(info)){
            return info;
        }
        try {
            JSONObject newObjec = new JSONObject();
            JSONArray objects = JSONArray.parseArray(info);
            JSONObject jsonObject = objects.getJSONObject(0);
            newObjec.put("id",jsonObject.get("id"));
            newObjec.put("type",jsonObject.get("type"));
            objects.clear();
            objects.add(newObjec);
            return  objects.toJSONString();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("控件格式转换错误！");
        }
        return info;
    }


    /**
     * @param date 时间字符串格式
     * @param format 时间格式
     * */
    public static String dateTransition(String date,String format){
        if (StringUtils.isEmpty(date)){
            return null;
        }

        try {
            date = date.replaceAll("[^\\d]","");
            int len = date.length();
            if(len >14){
                date= date.substring(0,14);
                len = date.length();
            }
            //初始值
            int year = 2020;
            int mon  = 1;
            int day  = 1;
            int hour = 0;
            int min  = 0;
            int sen  = 0;
            year = Integer.valueOf(date.substring(0,4));
            switch (len){
                //年
                case 4 : {
                    LocalDateTime dateTime = LocalDateTime.of(year, mon, day, hour,min);
                    return dateTime.format(DateTimeFormatter.ofPattern(format));
                }
                //年月
                case 6: {
                    mon = Integer.valueOf(date.substring(4,6));
                    LocalDateTime dateTime = LocalDateTime.of(year, mon, day, hour,min);
                    return dateTime.format(DateTimeFormatter.ofPattern(format));
                }
                //年月日
                case 8: {
                    mon = Integer.valueOf(date.substring(4,6));
                    day = Integer.valueOf(date.substring(6,8));
                    LocalDateTime dateTime = LocalDateTime.of(year, mon, day, hour,min);
                    return dateTime.format(DateTimeFormatter.ofPattern(format));
                }
                //年月日时
                case 10: {
                    mon = Integer.valueOf(date.substring(4,6));
                    day = Integer.valueOf(date.substring(6,8));
                    hour = Integer.valueOf(date.substring(8,10));
                    LocalDateTime dateTime = LocalDateTime.of(year, mon, day, hour,min);
                    return dateTime.format(DateTimeFormatter.ofPattern(format));
                }
                //年月日时分
                case 12: {
                    mon = Integer.valueOf(date.substring(4,6));
                    day = Integer.valueOf(date.substring(6,8));
                    hour = Integer.valueOf(date.substring(8,10));
                    min = Integer.valueOf(date.substring(10,12));
                    LocalDateTime dateTime = LocalDateTime.of(year, mon, day, hour,min);
                    return dateTime.format(DateTimeFormatter.ofPattern(format));
                }
                //年月日时分秒
                case 14: {
                    mon = Integer.valueOf(date.substring(4,6));
                    day = Integer.valueOf(date.substring(6,8));
                    hour = Integer.valueOf(date.substring(8,10));
                    min = Integer.valueOf(date.substring(10,12));
                    sen = Integer.valueOf(date.substring(12,14));
                    LocalDateTime dateTime = LocalDateTime.of(year, mon, day, hour,min,sen);
                    return dateTime.format(DateTimeFormatter.ofPattern(format));
                }

            }
        } catch (NumberFormatException e) {
            System.out.println("时间格式转化异常："+e.getMessage());
            System.out.println("传入时间："+date);
        }
        return null;
    }

    /**
     * * 判断一个对象是否非空
     *
     * @param object Object
     * @return true：非空 false：空
     */
    public static boolean isNotNull(Object object)
    {
        return !isNull(object);
    }
    /**
     * * 判断一个对象是否为空
     *
     * @param object Object
     * @return true：为空 false：非空
     */
    public static boolean isNull(Object object)
    {
        return object == null;
    }

}
